var t = getApp(), e = require("../../0BACB9C3A71FB5DF6DCAD1C4BE4CA4D1.js"), o = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), i = (require("../../B7F487A3A71FB5DFD192EFA4970CA4D1.js").BASE_URL, 
require("../../C6EFF1F4A71FB5DFA08999F3BD3CA4D1.js"));

i.getOptionsParams, i.navBack;

Page({
    data: {
        imgHeight: t.globalData.windowHeight - t.globalData.titleBarHeight - t.globalData.statusBarHeight,
        imgWidth: t.globalData.windowWidth,
        winWinth: t.globalData.windowWidth,
        myPrize: 0,
        isShowRules: !1,
        isShowLotteryResult: !1,
        curPos: 0,
        posList: [ 0, 1, 2, 7, -1, 3, 6, 5, 4 ],
        lotteryMsgList: [],
        isLotterying: !1,
        endLotteryTime: 100,
        userInfo: {},
        winLotteryMsg: {},
        getDataCount: 0,
        isAuthLoading: !1
    },
    onLoad: function(t) {
        this.getLotteryMsg(), this.getUserMsg(0);
    },
    onShow: function() {
        null !== e._cb && (e._cb = null, this.setData({
            isAuthLoading: !1
        })), this.data.getDataCount > 0 && (this.getLotteryMsg(), this.getUserMsg(1));
    },
    onReady: function() {
        var e = t.globalData.windowWidth, o = t.globalData.windowHeight - t.globalData.titleBarHeight - t.globalData.statusBarHeight, i = e, a = 2208 / 1242 * i;
        a < o && (i = 1242 * (a = o) / 2208), this.setData({
            imgHeight: a,
            imgWidth: i
        });
    },
    onShareAppMessage: function() {},
    lottery: function() {
        if (t.globalData.isLogin) {
            if (this.data.isLotterying) return;
            this.goToLottery();
        } else this.goToAuth();
    },
    goToLottery: function() {
        var t = this;
        1 * this.data.userInfo.lotteryDraw > 0 ? this.setData({
            isLotterying: !0
        }, function() {
            t.startLottery(), t.handleCurLottery();
        }) : wx.showModal({
            title: "提示",
            content: "抽奖次数已用完",
            showCancel: !1,
            confirmText: "确定"
        });
    },
    showLotteryResult: function() {
        this.setData({
            isShowLotteryResult: !0
        });
    },
    hideLotteryResult: function() {
        this.setData({
            isShowLotteryResult: !1
        });
    },
    showRules: function() {
        this.setData({
            isShowRules: !0
        });
    },
    hideRules: function() {
        this.setData({
            isShowRules: !1
        });
    },
    goToMyPrize: function() {
        t.globalData.isLogin ? wx.navigateTo({
            url: "/pages/lottery/myPrize"
        }) : this.goToAuth(function(t) {
            1001 === t && wx.navigateTo({
                url: "/pages/lottery/myPrize"
            });
        });
    },
    handleUsePrize: function() {
        this.hideLotteryResult();
        var t = this.data.winLotteryMsg;
        1 * t.type == 0 ? wx.navigateTo({
            url: "/pages/lottery/redeemPrize?code=" + t.code
        }) : wx.switchTab({
            url: "/pages/index/index"
        }), this.setData({
            winLotteryMsg: {}
        });
    },
    goToAuth: function(t) {
        var o = this;
        this.data.isAuthLoading ? wx.showToast({
            icon: "none",
            title: "数据加载中，请稍后请重试！"
        }) : (this.setData({
            isAuthLoading: !0
        }), e.login(function(e) {
            o.setData({
                isAuthLoading: !1
            }), "goods" in o.data.userInfo ? "function" == typeof t && t(e) : 1001 === e && o.getUserMsg(1, t);
        }));
    },
    startLottery: function() {
        var t = this;
        this.startTimer = setInterval(function() {
            var e = t.data.curPos;
            e = 7 === e ? 0 : e + 1, t.setData({
                curPos: e
            });
        }, 100);
    },
    handleEndLottery: function(t) {
        this.setData({
            isLotterying: !1
        }), this.startTimer && clearInterval(this.startTimer), this.endLotteryTimer && (this.endLotteryTimer && clearTimeout(this.endLotteryTimer), 
        "number" == typeof t && this.endLottery(t));
    },
    endLottery: function(t) {
        var e = this, o = this.data.endLotteryTime + 50, i = this.data.curPos;
        i = 7 === i ? 0 : i + 1, setTimeout(function() {
            if (o < 400 || i !== t) e.setData({
                curPos: i,
                endLotteryTime: o
            }, function() {
                e.endLottery(t);
            }); else if (i === t) {
                var a = e.data.userInfo;
                a.lotteryDraw = a.lotteryDraw - 1, a.goods = a.goods + 1, e.setData({
                    curPos: i,
                    isLotterying: !1,
                    endLotteryTime: 100,
                    userInfo: a
                }, function() {
                    setTimeout(function() {
                        e.setData({
                            isShowLotteryResult: !0
                        });
                    }, 1e3);
                });
            }
        }, o);
    },
    getUserMsg: function(e, i) {
        var a = this, s = {};
        t.globalData.authToken ? o.post("/v1/lottery/newUser", s).then(function(t) {
            1001 === t.data.code && a.setData({
                userInfo: t.data.response.data
            }, function() {
                "function" == typeof i && i(1001);
            });
        }) : 0 === e && this.goToAuth();
    },
    getLotteryMsg: function() {
        var t = this, e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
        0 === this.data.getDataCount && wx.showLoading({
            title: "加载中..."
        }), o.get("/v1/lottery/newGoods", e).then(function(e) {
            0 === t.data.getDataCount && (wx.hideLoading(), t.setData({
                getDataCount: 1
            })), 1001 === e.data.code && t.setData({
                lotteryMsgList: e.data.response.data
            });
        }).catch(function() {
            0 === t.data.getDataCount && (wx.hideLoading(), t.setData({
                getDataCount: 1
            }));
        });
    },
    handleCurLottery: function() {
        var t = this;
        this.getCurlottery(), this.endLotteryTimer && clearTimeout(this.endLotteryTimer), 
        this.endLotteryTimer = setTimeout(function() {
            t.handleEndLottery(), wx.showModal({
                title: "提示",
                content: "网络出错！",
                showCancel: !1
            });
        }, 2e4);
    },
    getCurlottery: function() {
        var t = this, e = Date.now();
        o.post("/v1/lottery/user/newLottery").then(function(o) {
            if (1001 === o.data.code) {
                var i = o.data.response.data;
                t.setData({
                    winLotteryMsg: i
                }, function() {
                    for (var o = t.data.lotteryMsgList, a = void 0, s = 0, n = o.length; s < n; s++) if (1 * o[s].id == 1 * i.goods_id) {
                        a = s;
                        break;
                    }
                    e - Date.now() < 1e3 && setTimeout(function() {
                        t.handleEndLottery(a);
                    }, 1e3);
                });
            } else t.handleEndLottery();
        }).catch(function() {
            t.handleEndLottery();
        });
    }
});